cd "\\micro.intra\Projekt\P0934$\P0934_Gem\Data Files\"
log using Kolfu, replace
*Open and clean the survey data
odbc load, exec("select * from Kolfu17_20171212del1och2") dsn("P0934") clear

collapse (firstnm)  Q01- v51_Tilltradesdatumvaldag_1, by(P0934_Lopnr_PersonNr)
ren P0934_Lopnr_PersonNr p_id
save "Kolfu survey", replace 

*Create data set with the number of periods previously nominated for office
use "\\micro.intra\Projekt\P0934$\P0934_Gem\Data Files\nom_ko_94_82.dta" , clear
destring ar ,replace force
append using "\\micro.intra\Projekt\P0934$\P0934_Gem\Data Files\nom_ko_98_14.dta"
duplicates drop P0934_Lopnr_personnr ar, force
ren P0934_Lopnr_personnr p_id
destring partikod, replace force
gen pk_2014b=partikod if ar==2014
bysort p_id: egen pk_2014=max(pk_2014b) 
gen nom_prev=1 if partikod==pk_2014 & partikod!=.
replace nom_prev=0 if ar==2014
collapse (sum) nom_prev, by(p_id) 
 
save "\\micro.intra\Projekt\P0934$\P0934_Gem\Data Files\nom_prev.dta" , replace

*Construct the labor market indices using adminastrative data, following the same proceducre as in the main data
clear

odbc load, exec("select * from Individ_2001") dsn("P0934") clear
gen year=2001
save kolfu_base, replace
forvalues year=2002/2015{	
	odbc load, exec("select * from Individ_`year'") dsn("P0934") clear
	gen year = `year'
	append using kolfu_base, force
	save kolfu_base, replace
}
ren P0934_Lopnr_PersonNr p_id
gen pbb = 44500 if year == 2015
replace pbb = 44400 if year == 2014
replace pbb = 44400 if year == 2013
replace pbb = 44000 if year == 2012
replace pbb	= 42800	if 	year ==	2011
replace	pbb	= 42400	if 	year ==	2010
replace	pbb	= 42800	if 	year ==	2009
replace	pbb	= 41000	if 	year ==	2008
replace	pbb	= 40300	if 	year ==	2007
replace	pbb	= 39700	if 	year ==	2006
replace	pbb	= 39400	if 	year ==	2005
replace	pbb	= 39300	if 	year ==	2004
replace	pbb	= 38600	if 	year ==	2003
replace	pbb	= 37900	if 	year ==	2002
replace	pbb	= 36900	if 	year ==	2001
replace pbb=pbb/100 
gen politician =  AstSNI2007 == "84111" | AstSNI2002 == "75111" | AstSNI92 == "75111"
gen BusPers=0 if YrkStalln!=""
replace BusPers=1 if YrkStalln=="4" |YrkStalln=="5" 
gen Employee=0 if YrkStalln!=""
replace Employee =1 if YrkStalln=="2" | YrkStalln=="1"
gen cap_exec = -1 if Employee==1
replace FInk= InkFNetto if FInk==.
replace cap_exec = 0.5 if PasNar!=0 | FInk!=0
replace cap_exec = 1   if (OpFtgLedare =="1" | BusPers ==1) 
replace cap_exec=. if ForvErs<(pbb*3.5) |politician==1
fasterxtile inc_pct_emp=ForvErs if  ForvErs>(pbb*3.5) & cap_exec ==-1 , by(year) n(100)
foreach var in  inc_pct_emp{
	replace  `var' = (`var'-50.5)/49.5
}
replace inc_pct_emp= inc_pct_emp/(2/1.5)-.25
gen cap_exec_alt= cap_exec
replace  cap_exec_alt= inc_pct_emp if cap_exec==-1
gen PrivSec =0 if SektorKod!="0"
replace PrivSec=1 if SektorKod=="21" |SektorKod=="22" |SektorKod=="25"
gen PubSec = (PrivSec-1)*-1
gen PubCom = 0 if SektorKod!="0"
replace PubCom = 1 if SektorKod=="12" |SektorKod=="23" |SektorKod=="24"
replace PubSec=0 if PubCom==1
gen NonProfit = 0 if SektorKod!="0"
gen InstKodsub = substr(InstKod7, 6,7)
replace NonProfit=1 if InstKodsub == "54" | InstKodsub == "61" | InstKodsub == "62" | InstKodsub == "63" | InstKodsub== "71" | InstKodsub== "72"| InstKodsub== "95"
replace PrivSec = 0 if NonProfit ==1
gen  sector = PrivSec - PubSec - PubCom*0 - NonProfit*0
replace sector=. if ForvErs<(pbb*3.5)|politician==1

do  "\\micro.intra\Projekt\P0934$\P0934_Gem\Do Files\Kitschelt\ssyk 2012 to 96 crosswalk.do"

replace Ssyk4= ssyk96kod if Ssyk4==""
replace Ssyk4="" if Ssyk4=="0000" |Ssyk4=="****" 
sort p_id year
foreach var in Ssyk4{
	replace `var'="" if ForvErs<(pbb*3.5) |politician==1
	replace `var'=`var'[_n-1] if `var'=="" &  p_id==p_id[_n-1]
}
foreach var in cap_exec sector    {
	replace `var'=`var'[_n-1] if `var'==. &  p_id==p_id[_n-1]
}
keep if year == 2015 
duplicates drop p_id year, force
gen sex= 1 if Kon=="2"
replace sex=-1 if Kon=="1"


drop ssyk96kod
gen ssyk96kod = substr(Ssyk4,1,3)

merge m:1 ssyk96kod using intcomp_ssyk.dta
drop ssyk96kod


*Define years of education
replace Sun2000niva="" if Sun2000niva=="*" | Sun2000niva=="-"
destring Sun2000niva, replace force
replace  Sun2000niva=. if Sun2000niva>=999
gen educ_year=.
replace educ_year= 7.5 if Sun2000niva>=100 & Sun2000niva<200
replace educ_year= 9.4 if Sun2000niva>=200 & Sun2000niva<300
replace educ_year= 11.2 if Sun2000niva>=300 & Sun2000niva<330
replace educ_year= 12.4 if Sun2000niva>=330 & Sun2000niva<400
replace educ_year= 14.2 if Sun2000niva>=410& Sun2000niva<530
replace educ_year= 17 if Sun2000niva>=530 & Sun2000niva<600
replace educ_year= 20.4 if Sun2000niva>=600 & Sun2000niva<=999
drop Sun*


gen int eduyrs = educ_year
gen education = .
replace education = -1 if eduyrs<10
replace education = -.5 if eduyrs==11
replace education = -.25 if eduyrs==12
replace education = 0.5 if eduyrs==14
replace education = 1 if eduyrs>15
replace education = . if missing(eduyrs)
gen  ssyk96kod = Ssyk4
drop _merge
joinby ssyk96kod using "SSYK96_Onet.dta", unmatched(master)
drop _merge
foreach var of varlist ConMacProc DocRecInfo HandMoveObj InterComp AdminAct{
	gen `var'_ = 100-`var'
}
egen galtan = rowmean(ConcOther AssCareOther NegConSolv ServOriCare SocPercep ConMacProc_ DocRecInfo_ HandMoveObj_ InterComp_ AdminAct_) 
replace galtan=. if Ssyk4==""

foreach var in   stdoffshore galtan{
	replace `var'=. if Ssyk4==""
	fastxtile `var'_pct = `var', n(100)
	replace  `var'_pct = (`var'_pct-50.5)/49.5
}
gen y = galtan_pct/2 + education/4 +sex/4
gen x = stdoffshore_pct/3+ sector/3 + cap_exec_alt/3 

joinby p_id  using "Kolfu survey.dta", unmatched(master)

foreach var in Q13 Q14 Q16{
	replace `var'= substr(`var', 1, 2)
}
destring q17* Q13* Q14* Q16*, replace force

gen  q17_12_rev=6-q17_12
gen  q17_13_rev=6-q17_13
gen x_pos =  (Q13/5-1)
gen y_pos = ( (Q14/5-1)+(Q16/5-1) )/2

egen std_y_pos=std(y_pos)
egen std_x_pos=std(x_pos)
foreach var in x_pos y_pos  {
	gen gr_`var'_8=(int(4*(`var'+0.0000001))-0.5)/4 if `var'<0
	replace gr_`var'_8=(int(4*(`var'-0.0000001))+0.5)/4  if `var'>=0
}

*create categorical variable for mode of recruitment 
gen recruit="own" if Q11=="Eget initiativ"
replace recruit="party" if Q11=="Kontaktad av politiskt parti"
replace recruit="asked other" if substr(Q11,1,4)=="Till"

*Create party initial
gen party_in="S" if substr(v09_Parti9,1,3)=="Soc"
replace party_in="G" if substr(v09_Parti9,1,3)=="Mil"
replace party_in="Cons" if substr(v09_Parti9,1,3)=="Mod"
replace party_in="SD" if substr(v09_Parti9,1,3)=="Sve"
gen parti_initial="S" if substr(v09_Parti9,1,3)=="Soc"
replace parti_initial="G" if substr(v09_Parti9,1,3)=="Mil"
replace parti_initial="M" if substr(v09_Parti9,1,3)=="Mod"
replace parti_initial="B" if substr(v09_Parti9,1,3)=="Sve"
gen valar=2014
drop _merge
*add data on previous terms nominated to council
joinby p_id  using "nom_prev.dta", unmatched(master)
destring Q10, force gen(memb_year)
gen memb_time=2017- memb_year
*drop regional councilors
drop if Q01=="Landstings-/regionfullmäktige"

cd "\\micro.intra\Projekt\P0934$\P0934_Gem\Do Files\Kitschelt\Submission"
*Figure 3
preserve
collapse y_pos x_pos , by(party_in )
twoway  (scatter y x if party_in=="S",  msize(zero) mlabel(party_in) mlabsize(medium) msymbol(circle) mlabposition(0) mfcolor(none) mlabcolor(cranberry)) ///
  (scatter y x if party_in=="Cons",  msize(zero) mlabel(party_in) mlabsize(medium) msymbol(circle) mlabposition(0) mfcolor(none) mlabcolor(navy)) ///
   (scatter y x if party_in=="G",  msize(zero) mlabel(party_in) mlabsize(medium) msymbol(circle) mfcolor(none) mlabposition(0) mlabcolor(forest_green)) ///
    (scatter y x if party_=="SD",  msize(zero) mlabel(party_in) mlabsize(medium) msymbol(circle) mlabposition(0) mfcolor(none) mlabcolor(gold))  ///
, scheme(s1color)  ylabel(-1(.5)1)  xlabel(-1(.5)1)  ///
 xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  ///
 title(Self-Reported Ideology of Councilors 2017) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)

graph save Figure_3, replace
restore

****Figure 4
 binscatter x_pos x   , n(50) scheme(s1color) ylabel(, angle(horizontal) grid)   title(Left-Right) xtitle( Labor Market Experience Index) ytitle(Self-reported Ideology)
  graph save xpos_x, replace
    
	binscatter y_pos y, n(50) scheme(s1color) title(Authoritarian-Libertarian) ylabel(, angle(horizontal) grid) xtitle(Labor Market Experience Index) ytitle( Self-reported Ideology)  
    graph save ypos_y, replace
     
   graph combine ypos_y.gph   xpos_x.gph,col(2) row(1) scheme(s1mono) ysize(6) xsize(20) iscale(1.5) 
 graph save Figure_4.gph, replace
*****Table W1 and Figure W2
*log using Table_W1, replace
bysort party_in: sum y x std_y std_x if y!=. & x!=.
bysort party_in recruit: sum y x std_y std_x if y!=. & x!=.
bysort party_in: sum y x std_y std_x 
bysort party_in recruit: sum y x std_y std_x
*log close


 binscatter std_y_pos memb_time, by(party_in) line(none) mcolors(navy forest_green maroon dkorange)
graph save membtime_ypos, replace
 binscatter std_x_pos memb_time, by(party_in) line(none) mcolors(navy forest_green maroon dkorange)
graph save membtime_xpos, replace

 binscatter std_y_pos nom_prev, by(party_in) line(none) discrete mcolors(navy forest_green maroon dkorange)
graph save nomprev_ypos, replace
 binscatter std_x_pos nom_prev, by(party_in) line(none) discrete mcolors(navy forest_green maroon dkorange)
graph save nomprev_xpos, replace 

   graph combine membtime_ypos.gph  membtime_xpos.gph  nomprev_ypos.gph  nomprev_xpos.gph    ///
, col(2) row(2) scheme(s1mono) ///
 ysize(16) xsize(20) iscale(.7) 
 graph save Figure_W2, replace

*Table W5
reg x_pos x
outreg2 using Table_W5, excel dec(2) se replace 

foreach var in x stdoffshore_pct sector cap_exec_alt y galtan_pct education sex {
	reg x_pos `var'
	outreg2 using Table_W5, excel dec(2) se append
}

foreach var in x stdoffshore_pct sector cap_exec_alt y galtan_pct education sex {
	reg y_pos `var'
	outreg2 using Table_W5, excel dec(2) se append
}


preserve
gen count=1

keep if gr_y_pos_8!=. & gr_x_pos_8!=.
collapse(sum) count, by(gr_y_pos_8 gr_x_pos_8 parti_initial)
bysort parti_initial: egen parti_count=sum(count)

gen share= count/parti_count
sum share
gen percent=round(share*1000)/10

twoway (scatter gr_y gr_x [fweight = share] if parti_initial=="B", mcolor(gold) mfcolor(none) msize(small) msymbol(circle)) ///
  (scatter gr_y gr_x  if parti_initial=="B" &percent>=1, mcolor(black)  mlabcolor(black)  msymbol(none) mlabel(percent) mlabposition(0)) ///
  , scheme(s1color) xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  title(Sweden Democrats) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)
graph save lab_circle_B, replace
  twoway (scatter gr_y gr_x [fweight = share] if parti_initial=="G", mcolor(forest_green) mfcolor(none) msize(small) msymbol(circle)) ///
  (scatter gr_y gr_x  if parti_initial=="G" &percent>=1, mcolor(black)  mlabcolor(black)  msymbol(none) mlabel(percent) mlabposition(0)) ///
  , scheme(s1color) xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  title(Green Party) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)
graph save lab_circle_G, replace
  twoway (scatter gr_y gr_x [fweight = share] if parti_initial=="M", mcolor(navy) mfcolor(none) msize(small) msymbol(circle)) ///
  (scatter gr_y gr_x  if parti_initial=="M" &percent>=1, mcolor(black)  mlabcolor(black)  msymbol(none) mlabel(percent) mlabposition(0)) ///
  , scheme(s1color) xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  title(Conservatives) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)
graph save lab_circle_M, replace
  twoway (scatter gr_y gr_x [fweight = share] if parti_initial=="S", mcolor(cranberry) mfcolor(none) msize(small) msymbol(circle)) ///
  (scatter gr_y gr_x  if parti_initial=="S" & percent>=1, mcolor(black)  mlabcolor(black)  msymbol(none) mlabel(percent) mlabposition(0)) ///
  , scheme(s1color) xline(0, lpattern(dash) lcolor(black)) yline(0, lpattern(dash) lcolor(black))  title(Social Democrats) ytitle( Authoritarian-Libertarian) xtitle(Left-Right) legend(off) ysize(5) xsize(5.5)

graph save lab_circle_S, replace
 
   graph combine  lab_circle_G.gph  lab_circle_B.gph  lab_circle_S.gph  lab_circle_M.gph ///
, col(2) row(2) scheme(s1mono) ysize(16) xsize(20) iscale(.7)  ycommon xcommon
 
  graph save Figure_W3, replace
  
restore
log close
